package global

import (
	"github.com/zachary/tbox/internal/web/util"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
)

// UnauthorizedCallback 无权访问回调
func UnauthorizedCallback(w http.ResponseWriter, r *http.Request, err error) {
	if err != nil {
		logx.Errorf("authentication error: %v", err)
	}
	httpx.OkJson(w, util.Fail("authentication error"))
}

func ErrorHandlerCallback(err error) (int, any) {
	switch e := err.(type) {
	case *util.R:
		return http.StatusOK, &util.R0{Code: e.Code, Msg: e.Msg, Data: e.Data}
	default:
		return http.StatusInternalServerError, &util.R0{Code: http.StatusInternalServerError, Msg: e.Error()}
	}
}
